<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>
<body>
<input type="file" name="">
<img src="" alt='待上传'>
<script type="text/javascript">
	let inp = document.querySelector('input')
	let img = document.querySelector('img')
	inp.onchange = function() {
		console.log(inp.files) //上传图片的信息 

		//FormData 用来处理文件上传 通常用来发送表单数据
		let fileParam = new FormData()
		//append 向FormData里添加一个新的键值对 key:value
		fileParam.append('file',inp.files[0])

		let xhr = new XMLHttpRequest()

		xhr.open('post','http://joint.byesame.com/upload')
		//不用加请求
		xhr.send(fileParam)
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4){
				let res = JSON.parse(xhr.responseText)
				console.log(res)
				//http://localhost:3002/upload/1745302363898.jpg
				let str = res.data.path.replace("http://localhost:3002","http://joint.byesame.com") 
				img.src = str
			}
		}
	}
</script>
</body>
</html>